//package com.example.gateway.filter;
//
//import com.example.gateway.cinfig.ConfigProperties;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.annotation.Order;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.net.InetSocketAddress;
//
///**
// *  利用 GlobalFilter 模拟登录拦截
// *
// *  请求参数中含有, token, 且值为 lijiaqiang , 才放行
// */
//@Order(-1)  // 填入一个值, 值越小, 就越优先执行(可能存在多个过滤器)
//@Component
//public class AuthorizeFilter implements GlobalFilter {
//
//    @Autowired
//    private ConfigProperties patternProperties;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//        // 1. 获取请求端口
//        ServerHttpRequest request = exchange.getRequest();
//        InetSocketAddress address = request.getLocalAddress();
//        System.out.println(address == null);
//        if (address == null ) {
//            // 拦截请求
//            Mono<Void> voidMono = exchange.getResponse().setComplete();   // 返回值就是 Mono<Void>
//
//            return voidMono;
//        }
//
//        int port = address.getPort();
//        System.out.println(port + "  " + (patternProperties.getPort().equals(port+"")));
//        // 3. 是否等于 lijiaqiang
//        if (patternProperties.getPort().equals(port+"")) {
//            // 4. 是, 放行
//            Mono<Void> filter = chain.filter(exchange);  // 返回值就是 Mono<Void>
//            return filter;
//        }
//        //  否, 拦截
//        // 设置状态码, 增强用户体验,  HttpStatus.UNAUTHORIZED 表示 401 , 没有权限
//        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//        // 拦截请求
//        Mono<Void> voidMono = exchange.getResponse().setComplete();   // 返回值就是 Mono<Void>
//
//        return voidMono;
//    }
//
//}
